package com.example.blog.common.interceptor;

import com.example.blog.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/29
 * Time: 16:17
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 校验 token 是否正确
        // 获取 token
        String token = request.getHeader("user_header_token");
        log.info("从 header 中获取到 token, token:{}", token);
        Claims claims = JwtUtil.parseToken(token);
        // token 不合法
        if (claims == null) {
            response.setStatus(401);
            return false;
        }
        // TODO 也可以做细一点
        // 比如校验id和name是否匹配
//        claims.get("id")
        return true;
    }
}
